<?php

namespace app\api\model;

use app\api\model\YaoqingRecords as Income;

class ConfigInvite extends Common
{
    static public function index($user, $types=0)
    {
        $ConfigInviteModel = new ConfigInvite;

        $config = ['income' => false, 'topup' => false];

        $yqms = [];

        $team = [$user['yqm']];
        for ($m = 1; $m <= 2; $m++) {
            $chids = User::where(['yqr' => ['in', $team]])->column('yqm');

            if ($chids) {
                $yqms = array_merge($yqms, $chids);

                $team = $chids;
            } else {
                $m = 4;
            }
        }

        if ($yqms) {
            $ids = User::where(['yqm' => ['in', $yqms]])->column('id');

            if ($ids) {
                if (in_array($types, [0,1])) {
                    /*$performance = Income::where(['uid' => ['in', $ids], 'touid' => ['neq', 0], 'content' => ['like', '%收益'], 'type' => 1, 'data' => 0])->where(['content' => ['not like', '充值%']])->sum('money');

                    $performance = $performance ?: 0;*/

                    $performance = (new UserData)->where(['user_id' => ['in', $ids]])->sum('income');

                    $data = $ConfigInviteModel->where(['types' => 1, 'min' => ['elt', $performance]])->order('min desc')->find();

                    if ($data) {
                        $config['income'] = $data;
                    }
                }

                if (in_array($types, [0,2])) {
                    /*$performance = Topup::where(['fk_userid' => ['in', $ids], 'type' => ['neq', 3], 'state' => 1])->sum('price');

                    $performance = $performance ?: 0;*/

                    $performance = (new UserData)->where(['user_id' => ['in', $ids]])->sum('topup');

                    $data = $ConfigInviteModel->where(['types' => 2, 'min' => ['elt', $performance]])->order('min desc')->find();

                    if ($data) {
                        $config['topup'] = $data;
                    }
                }
            }
        }

        return $config;
    }

    static public function zhiyao($user)
    {
        $config = ['income' => 0, 'topup' => 0];

        $yqms = [];

        $ids = User::where(['yqr' => $user['yqm']])->column('id');

        if ($ids) {
            $performance = (new UserData)
                ->where(['user_id' => ['in', $ids]])
                ->field('sum(income) income,sum(topup_volley) topup')
                // ->fetchSql(true)
                ->find();
            // var_dump($performance);exit;


            if ($performance) {
                $config['income'] = $performance['income'];
                $config['topup'] = $performance['topup'];
            }
        }

        return $config;
    }
}
